﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AusProject.OrderDataControls;
using AusProject.OrderComponents;


namespace AusProject
{
    public partial class FormOrderSummaryRuntime : Form
    {
        public FormOrderSummaryRuntime()
        {
            InitializeComponent();
        }


        public void LoadForm(OrderSummaryFormInfo formInfo, bool isSavedForm)
        {
            panelMainTitle.Width = formInfo.Width;
            panelMainTitle.Height = formInfo.MainTitleHeight;

            panelMainTitle.Width = formInfo.Width;
            panelMainTitle.Height = formInfo.MainTitleHeight;

            panelMainTitle.Controls.Clear();
            foreach (DataBoxInfo dboxInfo in formInfo.MainTitleDataBoxes)
            {
                DataBox dbox = dboxInfo.CreateDataBox();
                dbox.Mode = DataControlMode.Running;
                if (!isSavedForm)
                {
                    dbox.Value = dbox.DefaultValue;
                }
                panelMainTitle.Controls.Add(dbox);
            }

            flowSummaryForm.Width = formInfo.Width;
            flowSummaryForm.Controls.Clear();
            flowSummaryForm.Controls.Add(panelMainTitle);
            foreach (OrderComponentInfo ocInfo in formInfo.OrderComponents)
            {
                OrderComponent oc = new OrderComponent();
                oc.ImportInfo(ocInfo);

                oc.BackColor = SystemColors.Window;
                oc.MouseClick += new MouseEventHandler(oc_MouseClick);
                oc.RowClicked += new EventHandler<RowClickedEventArgs>(oc_RowClicked);

                flowSummaryForm.Controls.Add(oc);
            }
        }


        OrderComponent selectedOrderComponent = null;
        ComponentRow selectedOrderComponentRow = null;


        void oc_RowClicked(object sender, RowClickedEventArgs e)
        {
            if (selectedOrderComponent != null)
            {
                selectedOrderComponent.Unhighlight();
            }
            if (selectedOrderComponentRow != null)
            {
                selectedOrderComponentRow.Unhighlight();
            }

            selectedOrderComponent = (OrderComponent)sender;
            selectedOrderComponent.Highlight();

            if (!e.Row.IsLastLine)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    cmnuSectionRow.Show(((Control)sender).PointToScreen(e.Location));
                }
                selectedOrderComponent.Unhighlight();
                selectedOrderComponentRow = e.Row;
                e.Row.Highlight();
            }
            else
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    cmnuSectionTitle.Show(((Control)sender).PointToScreen(e.Location));
                }
            }
        }

        void oc_MouseClick(object sender, MouseEventArgs e)
        {
            if (selectedOrderComponent != null)
            {
                selectedOrderComponent.Unhighlight();
            }
            if (selectedOrderComponentRow != null)
            {
                selectedOrderComponentRow.Unhighlight();
            }

            selectedOrderComponent = (OrderComponent)sender;
            selectedOrderComponent.Highlight();

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                cmnuSectionTitle.Show(((Control)sender).PointToScreen(e.Location));
            }
        }

        private void addSupplierOrderFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormSupplierOrderFormChooser chooser = new FormSupplierOrderFormChooser();
            chooser.SetComponentType(selectedOrderComponent.Id);
            chooser.ShowDialog();

            if (chooser.Accepted)
            {
                ComponentRow addedRow = selectedOrderComponent.LastRow;

                addedRow.Data = chooser.SelectedForm;
                selectedOrderComponent.AddRow();

                selectedOrderComponent.Unhighlight();
                addedRow.Highlight();
                selectedOrderComponentRow = addedRow;

                FormSupplierOrderRuntime runForm = new FormSupplierOrderRuntime();
                runForm.LoadForm(chooser.SelectedForm, false);
                runForm.ShowDialog();

                if (runForm.Accepted)
                {
                    SupplierOrderFormInfo formInfo = runForm.ExportSupplierOrderFormInfo();
                    addedRow.Data = formInfo;
                    UpdateSummaryFields(selectedOrderComponent, addedRow, formInfo);
                }
            }
        }

        private void UpdateSummaryFields(OrderComponent oc, ComponentRow row, SupplierOrderFormInfo formInfo)
        {
            for (int i = 0; i < oc.Columns.Length; i++)
            {
                for (int k = 0; k < formInfo.ListOfDataBox.Count; k++)
                {
                    if (formInfo.ListOfDataBox[k].SummaryFieldId == oc.Columns[i].Id)
                    {
                        row.SetTextOfCol(i, formInfo.ListOfDataBox[k].Value);
                        break;
                    }
                }
            }
        }

        private void editThisFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ComponentRow editingRow = selectedOrderComponentRow;

            FormSupplierOrderRuntime runForm = new FormSupplierOrderRuntime();
            runForm.LoadForm((SupplierOrderFormInfo)editingRow.Data, true);
            runForm.ShowDialog();

            if (runForm.Accepted)
            {
                SupplierOrderFormInfo formInfo = runForm.ExportSupplierOrderFormInfo();
                editingRow.Data = formInfo;
                UpdateSummaryFields(selectedOrderComponent, editingRow, formInfo);
            }
        }

        private void deleteThisFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete this row and the attached supplier order form?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                selectedOrderComponent.RemRow(selectedOrderComponentRow);
            }
        }

        private void flowSummaryForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (selectedOrderComponent != null)
            {
                selectedOrderComponent.Unhighlight();
                selectedOrderComponent = null;
                if (selectedOrderComponentRow != null)
                {
                    selectedOrderComponentRow.Unhighlight();
                    selectedOrderComponentRow = null;
                }
            }
        }


        private bool accepted = false;

        public bool Accepted
        {
            get
            {
                return accepted;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            accepted = true;
            this.Close();
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            accepted = false;
            this.Close();
        }


        public void SetTitle(string formTitle, int formID)
        {
            string oFormTitle = string.IsNullOrEmpty(formTitle) ? "" : " - " + formTitle;
            string oFormID = formID >= 0 ? " (#" + formID + ")" : " (NoID)";
            this.Text = "Order Summary Form" + oFormTitle + oFormID;
        }
    }
}